package com.cx.controller;


import com.cx.common.ApiReturn;
import com.cx.pojo.Comment;
import com.cx.pojo.Responses;
import com.cx.pojo.User;
import com.cx.service.CommentService;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("comment")
public class CommentController {
//应当是只有增删查操作,由于responses是嵌套在comment中的，所以对responses的增删操作就是对comment的修改操作。
    //所以只涉及评论的增，删，改（即添加回复以及删除回复）
    @Autowired
    CommentService commentService;
    @Autowired
    UserCenterService userCenterService;

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm");

    @RequestMapping("insertComment")
    @ResponseBody
    public ApiReturn insertComment(String commentText,String postId,String userName,HttpServletRequest request) {
        //添加评论
        //时间格式如下存入mongoDB中
        String dateStr = sdf.format(calendar.getTime());
        Comment comment = new Comment();
        //UUID做唯一标识符
        comment.setComment_id(UUID.randomUUID().toString());
        User user = userCenterService.selectByUsetName(userName);
        comment.setComment_blog_id(postId);
        comment.setComment_user_id(String.valueOf(user.getUserId()));
        comment.setComment_user_name(user.getUserName());
        comment.setComment_user_img(user.getUserPhoto());
        comment.setComment_content(commentText);
        comment.setComment_create_time(dateStr);
        commentService.insertComment(comment);
            return ApiReturn.success("插入成功");
    }


    @RequestMapping("deleteComment")
    @ResponseBody
    public ApiReturn deleteComment(String comment_id){
        //只有作者自己发表的评论才能删除，先判断此评论的userId是否等于当前session中的userId，相同则渲染删除按钮。
        int affectrows = commentService.deleteComment(comment_id);
        if(affectrows == 1) {
            return ApiReturn.success("删除成功");
        }else{
            return ApiReturn.success("删除失败");
        }
    }

    @RequestMapping("addResponse")
    @ResponseBody
    public ApiReturn addResponse(String comment_id, String responseText,String userName, HttpServletRequest request){
        //添加回复需要评论id以及回复内容(前端获取)，和当前回复者的信息(后端获取)

        String dateStr = sdf.format(calendar.getTime());
        Responses response = new Responses();
        response.setId(UUID.randomUUID().toString());

        /*response.setResponse_user_id("003");
        response.setResponse_user_name("王笑舒");
        response.setResponse_user_img("11111");
        response.setResponse_content(responseText);
        response.setResponse_create_time(dateStr);*/

        User user = userCenterService.selectByUsetName(userName);
        response.setResponse_user_id(String.valueOf(user.getUserId()));
        response.setResponse_user_name(user.getUserName());
        response.setResponse_user_img(user.getUserPhoto());
        response.setResponse_content(responseText);
        response.setResponse_create_time(dateStr);

        int affectrows = commentService.addResponse(comment_id,response);
        if(affectrows == 1) {
            return ApiReturn.success("添加回复成功");
        }else{
            return ApiReturn.error("添加回复失败");
        }
    }

    @RequestMapping("deleteResponse")
    @ResponseBody
    public ApiReturn deleteResponse(String comment_id,String response_id){
        int affectrows = commentService.deleteResponse(comment_id,response_id);
        if(affectrows == 1) {
            return ApiReturn.success("删除回复成功");
        }else{
            return ApiReturn.error("删除回复失败");
        }
    }

    @RequestMapping("findByPostId")
    @ResponseBody//String postId
    public ApiReturn findByPostId(String postId,HttpServletRequest request){
        //直接用session获取当前文章的id，无需从前端进行传值
        //        //String postId = (String) request.getSession().getAttribute("postId");
        //        //postId = "03";
        List<Comment> comments = commentService.findByPostId(postId);
        return ApiReturn.success("查询成功",comments);
    }
}
